package com.his.modules.business.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.his.core.utils.StringUtils;
import com.his.modules.business.entity.MedicalRecordAnalysis;
import com.his.modules.business.entity.MedicalRecordAnalysisRecord;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MedicalRecordAnalysisUtil {
    /**
     * 新病历xml串解析
     * @param xml
     */
    public static List<MedicalRecordAnalysisRecord> medicalRecordAnalysis(String xml, MedicalRecordAnalysis medicalRecordAnalysis) {
        List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList = new ArrayList<>();
        if(StringUtils.isNotBlank(xml)) {
            JSONObject jsonObject = XML.toJSONObject(xml);
            if(null != jsonObject) {
                JSONObject xTextDocument = jsonObject.getJSONObject("XTextDocument");
                JSONObject xElements = xTextDocument.getJSONObject("XElements");
                JSONArray xElementList = xElements.getJSONArray("Element");
                String bodyText = xTextDocument.getStr("BodyText");
                if(StringUtils.isNotBlank(bodyText)) {
                    medicalRecordAnalysis.setWbnr(bodyText.getBytes());
                }
                if(xElementList.size() > 0) {
                    for(int i = 0; i < xElementList.size(); i ++) {
                        JSONObject elementDoc = xElementList.getJSONObject(i);
                        String xsiType = elementDoc.getStr("xsi:type");
                        if("XTextBody".equals(xsiType)) {
                            getAllElements(elementDoc, medicalRecordAnalysisRecordList, null);
                        }
                    }
                }
            }
        }
        return medicalRecordAnalysisRecordList;
    }

    /**
     * 递归调用获取节点信息
     * @param jsonObject
     * @param medicalRecordAnalysisRecordList
     */
    public static void getAllElements(JSONObject jsonObject, List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList, JSONObject jsParent) {
        String xsiType = jsonObject.getStr("xsi:type");
        Object obj = jsonObject.get("XElements");
        JSONObject jsPrt = null;

        if("XInputField".equals(xsiType)) {
            MedicalRecordAnalysisRecord medicalRecordAnalysisRecord = new MedicalRecordAnalysisRecord();
            medicalRecordAnalysisRecord.setYsid(jsonObject.getStr("ID"));
            medicalRecordAnalysisRecord.setYsmc(jsonObject.getStr("Name"));
            medicalRecordAnalysisRecord.setYsqz(jsonObject.getStr("InnerValue"));
            //如果父元素（段落元素）不为空，就set下值
            if(null != jsParent) {
                medicalRecordAnalysisRecord.setDlid(jsParent.getStr("dlid"));
                medicalRecordAnalysisRecord.setDlmc(jsParent.getStr("dlmc"));
                medicalRecordAnalysisRecord.setDlqz(jsParent.getStr("dlqz"));
            }
            medicalRecordAnalysisRecordList.add(medicalRecordAnalysisRecord);
            //判断下当前元素是不是段落元素，是的话，扔到下次遍历给下次遍历当爹
            if(isParagraph(jsonObject)) {
                jsPrt = new JSONObject();
                jsPrt.set("dlid", jsonObject.getStr("ID"));
                jsPrt.set("dlmc", jsonObject.getStr("Name"));
                jsPrt.set("dlqz", jsonObject.getStr("InnerValue"));
            }
        }

        if(Objects.isNull(obj) || "".equals(obj.toString())) {
            return;
        }
        JSONObject xElements = (JSONObject)obj;
        JSONArray xElementList = new JSONArray();
        try {
            xElementList = xElements.getJSONArray("Element");
        } catch (Exception e) {
            JSONObject element = xElements.getJSONObject("Element");
            xElementList.add(element);
        }
        //竟然有为null的情况
        if(!Objects.isNull(xElementList)) {
            if(xElementList.size() > 0) {
                for(int i = 0; i < xElementList.size(); i ++) {
                    getAllElements(xElementList.getJSONObject(i), medicalRecordAnalysisRecordList, jsPrt);
                }
            }
        }
    }

    /**
     * 判断是否段落
     * @param jsonObject
     * @return
     */
    public static boolean isParagraph(JSONObject jsonObject) {
        String name, value;
        Object obj = jsonObject.get("Attributes");
        if(Objects.isNull(obj) || "".equals(obj.toString())) {
            return false;
        }
        JSONObject attributes = (JSONObject)obj;
        JSONArray attributeList = new JSONArray();
        try {
            attributeList = attributes.getJSONArray("Attribute");
        } catch (Exception e) {
            JSONObject attribute = attributes.getJSONObject("Attribute");
            attributeList.add(attribute);
        }
        if(attributeList.size() > 0) {
            for(int i = 0; i < attributeList.size(); i ++) {
                name = attributeList.getJSONObject(i).getStr("Name");
                value = attributeList.getJSONObject(i).getStr("Value");
                if("段落标记".equals(name) && "1".equals(value)) {
                    return true;
                }
            }
        }
        return false;
    }
}